package com.ustadzmenjawab.server;


import java.util.List;
import java.util.TreeMap;



import com.ustadzmenjawab.client.AppControlService;
import com.ustadzmenjawab.indexing.QuerySpellCheck;
import com.ustadzmenjawab.shared.Article;
import com.ustadzmenjawab.shared.SearchResult;
import com.ustadzmenjawab.shared.WebsiteSource;

@SuppressWarnings("serial")
public class AppControlServiceImpl extends com.google.gwt.user.server.rpc.RemoteServiceServlet implements
		AppControlService {

	@Override
	public void addNewArticle(Article article) {
		if(article == null)
			return ;
		DBAccess db = new LuceneAccess();
		db.insertArticle(article);
	}

	@Override
	public List searchKeyword(String keyword) {
		if(keyword == null)
			return null;
		DBAccess db = new LuceneAccess();
		String ip = getThreadLocalRequest().getHeader("X-Forwarded-For"); 
		if(ip == null || ip.length() == 0){
			ip = getThreadLocalRequest().getRemoteAddr();
		}
		List<SearchResult> ls = db.searchDBForKeyword(keyword,ip);
		return ls;
	}
	@Override
	public	List searchKeyword(String keyword,String source) {
		if(keyword == null || source == null)
			return null;
		DBAccess db = new LuceneAccess();
		List<SearchResult> ls = db.searchDBForKeywordWithSource(keyword,source);
		return ls;
	}
	@Override
	public List searchKeyword(String keyword, int start, int size) {
		if(keyword == null )
			return null;
		DBAccess db = new LuceneAccess();
		List<SearchResult> ls = db.searchDBForKeyword(keyword,start,size);
		return ls;
	}
	
	@Override
	public List searchKeyword(String keyword,String source, int start, int size) {
		if(keyword == null || source == null)
			return null;
		DBAccess db = new LuceneAccess();
		List<SearchResult> ls = db.searchDBForKeyword(keyword,source,start,size);
		return ls;
	}

	@Override
	public List<String> getSourcesForResult(String keyword) {
		DBAccess db = new LuceneAccess();
		return db.getSourcesForResult(keyword);
	}

	@Override
	public int getTotalCount(String keyword) {
		DBAccess db = new LuceneAccess();
		return db.getTotalCount(keyword);
	}

	@Override
	public int getTotalCountForSource(String keyword, String source) {
		DBAccess db = new LuceneAccess();
		return db.getTotalCount(keyword, source);
	}

	@Override
	public void updateVisit(String url) {
		DBAccess db = new LuceneAccess();
		db.updateVisit(url);
	}

	@Override
	public String getQuestion(int id) {
		DBAccess db = new LuceneAccess();
		return db.getQuestion(id);
	}

	@Override
	public List<SearchResult> searchSimilarQuestion(String keyword, int qid) {
		DBAccess db = new LuceneAccess();
		
		return db.searchSimilarQuestion(keyword, qid);
	}

	@Override
	public List<SearchResult> searchSimilarQuestion(String keyword, int qid,
			int start, int size) {
		DBAccess db = new LuceneAccess();
	
		return db.searchSimilarQuestion(keyword, qid, start, size);
	}

	@Override
	public int getCountSimilarResult(int qid) {
		DBAccess db = new LuceneAccess();
		
		return db.getCountSimilarResult(qid);
	}

	@Override
	public String getPage(String pageName) {
		DBAccess db = new DBAccess();
		return db.getPage(pageName);
	}

	@Override
	public List<WebsiteSource> getSourceList() {
		DBAccess db = new DBAccess();
		return db.getSourceList();
	}

	@Override
	public String getSpellSuggestion(String query) {
		return QuerySpellCheck.getSpellSuggestion(query);
	}

	

}
